<?php

/**
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class UeditorAction extends AttachmentsAction {

    public $isadmin;
    public $maxSize;
    public $allowExts;

    function _initialize() {
        //判断前台还是后台
        $this->isadmin = session(C("USER_AUTH_KEY")) ? true : false;
        
        //以下方法不管前台还是后台都可以使用
        if (in_array(ACTION_NAME, array("getRemoteImage", "imageManager", "snapImgUp", "imageUp", "getMovie"))) {
            C("USER_AUTH_ON", false);
            $this->initUser();
            import('@.ORG.Util.UploadFile');
            //检查是否登陆
            if (empty($this->Cache['uid'])) {
                $this->error("请登陆后操作！！");
            }
            parent::_initialize();
            if ($this->isadmin) {
                //设置上传大小
                $this->maxSize = (int) CONFIG_UPLOADMAXSIZE * 1024; //单位字节
            } else {
                $this->maxSize = (int) CONFIG_QTUPLOADMAXSIZE * 1024; //单位字节
            }
            $this->module = strtolower("contents");
            //上传目录
            $this->filepath = $this->FilePath();
        } else {
            C("USER_AUTH_ON", true);
            parent::_initialize();
        }
    }

    //处理远程图片抓取的地址
    public function getRemoteImage() {
        
    }

    //图片在线浏览的处理地址
    public function imageManager() {
        $data = $this->att_not_used();
        $str = "";
        foreach($data as $v){
            $str .= $v['src']."ue_separate_ue";
        }
        echo $str;
    }

    //屏幕截图的server端保存程序
    public function snapImgUp() {
        //array2file($_POST, './post.txt');
    }

    /**
     * 编辑器图片上传
     * array (
      'pictitle' => '6.jpg',
      'Filename' => '6.jpg',
      'catid' => '73',
      'Upload' => 'Submit Query',
      )
     */
    public function imageUp() {
        if ($this->isPost()) {
            $upload = new UploadFile();
            //设置上传类型，强制为图片类型
            $upload->allowExts = $this->allowExts = array("jpg", "png", "gif", "jpeg");
            //设置上传目录
            $upload->savePath = $this->filepath;
            $this->catid = $this->_post("catid");
            if(empty($this->catid)){
                 echo "{'url':'图片地址','state':'参数错误！','title':'标题'}";
                 exit;
            }
            //开始上传
            if ($upload->upload()) {
                //上传成功
                $info = $upload->getUploadFileInfo();
                $status = $this->FileData($info[0]);
                //是否添加水印 
                if (CONFIG_WATERMARKENABLE) {
                    $this->water($info[0]['savepath']);
                }
                $in = array(
                    "url" => "",
                    "state" => "",
                    "title" => ""
                );
                
                if ($status) {
                    // 设置附件cookie
                    $this->upload_json($status,$this->filehttp,str_replace(array("\\", "/"), "", $info[0]['name']));
                    
                    $in['url'] = $this->filehttp;
                    $in['title'] = str_replace(array("\\", "/"), "", $info[0]['name']);
                    $in['state'] = "SUCCESS";
                    echo json_encode($in);
                    exit;
                } else {
                    //删除已经上传的图片，这里逻辑还要优化
                    @unlink($this->filepath);
                     echo "{'url':'图片地址','state':'上传失败！','title':'标题'}";
                     exit;
                }
            }
        }
        echo "{'url':'图片地址','state':'上传失败！','title':'标题'}";
        exit;
    }

    /**
     * 视频搜索 
     */
    public function getMovie() {
        $key = $this->_post("searchKey");
        $type = $this->_post("videoType");
        $html = file_get_contents('http://api.tudou.com/v3/gw?method=item.search&appKey=myKey&format=json&kw=' . $key . '&pageNo=1&pageSize=20&channelId=' . $type . '&inDays=7&media=v&sort=s');
        echo $html;
    }

}

?>
